package com.tjh.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.tjh.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * Create by koala on 2021-05-29
 */

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private String app_id = "2016091900550956";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private String merchant_private_key = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCww1P+/GRaXqR+nBHhckoCl3aDsj6+ttWaujKLLhtVH5TG8P3AA85oD461AcRP6uXZ8h4IEQ02/Xpwv43Q+u7IqSpvrpZEqhbNxRoQMk57kroeOTQiMNOFO7TQ/KnKJtjxGr2v/GgbBpTX2Fn4hvkOmP7jjpfTlbo0g3mk6GqqKCirRl/jAEm5/LpH3j8oWeO5kPAQc4d3fscezUJ/MNdeT/+lzeYMPzlzr6lHtwrfqzQ2t61rY9NKLKFS/wZg3xu5pf/lApd1/pXtUYOSCHK6C1zF8esuxcUJFcuhBmhIanTjgR5AQWiiVpJi2ERBW34h2IOG5RJ4JY5fmQtmOUJRAgMBAAECggEAW4cY5EyubfjZtlF8FL/IQOVK0dCnGruziWXmrBjYwFAVCppY2mdeJpX/sAxVKYiZMixjjou9yv6fuugFN/PnVvH5blD5sF5eZngsEbzq1KPaSbIXI6kirbeH3jsmc+1/ogDVvd21zmCvC8EUfVLufbeNLCl2xnGeNHjrsQ9DxA0fMy7ZneM+iEhlIXx5AAHDbBeWw5YanihZy/nxZZJC04Pq5Gh3oRZ1qF3uT0h06lmg9IJIwWQiYAchUsxTEl3HrkR6ia8iX2gcDjnzwH2JEjKJpsk4Gs2GPOUslBMmsJgmJ5P3e0lvUyBOBqGmKv9J7R7APPsNfyTEjedDFSmZnQKBgQDyJpl7xGxwnYEqeo0HjfE6camvd8x/o/XEObjoKES1P7sUz0otdkRCqaJjzAoBL/eOWitvHzNPT8qqzrH//GIJeIlDvblQ1MMgmm9S9RC1fM0Ohf4UTPWKSwZt1YiOqHRBo1tc0zAFaFf5aE4FxwujoEu8iXtQa+JO8ADLuU4uHwKBgQC6313wV6mrUuG9ZdlZfIeMzdJGJbFm3I7QF5o+3loU54N+3FAi0kzW0800Pmv4hJ1kKzSglMUCl7C/g+dZpB1Hg3GoShDXDWwa4NIbSns1e3+AKEnY8uz0wU7tWiezh8jyVoOXxk33r1lk3ckONwpFETHJ1gB+ywtT3+ig3fShjwKBgHCCQrngz8mbRNJp0nD6IGTZPtAKR3fexGgBscocO6BDFjdbAMoSszCOBKd9vABFOy5m8ttUkdB98FpgYw/fQeXInW5eDmZiFK9nUeYnezX34Iao9x6Ti1pP9ZK+rg4qcqm0rtR4Q02f55A4SFgi3WOPLOFSt3rsRWAL3zLIzGxdAoGAB7LkH968sr47Zp4nkjQ7MWXZX+J1qJM1nwUZ6bUnaO0Q0c0p0AVx/PfE4H4h3J86DMJq7KK0ayLsuTcp9Nt6CVQ0/oXuo6194q2Yel9bLBzmZ9VBFZsMgBjms4ifUhoqATHnQf19gNLMkAdDHT7PQYEv1+zlIFQlJ1N2Ngpu5akCgYBlKklZ1Erc30PvScq+zaHrnlUqQXgUxVX3JUaaR1AWLrK3zK3SXXAwzVmUDi94drzRDPIBvA2NydyAkGesMrou7vccNvBLH0s2atnrN6XzlGn6GDH9jDMXnVhZZrV/CjfVki+fWcFyedUPqTam+wy+r7HlOamANM/+hbYh9TO1Fw==";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuqTPbxQh5iOh3CN8bFbWLF4UI4gjl0TXJlCOjImuZZG0ZhhZwk6cQ5U75E52Z8VqYVpasAxnyoZJkubCFekaG+XuFdM2Mmb6W3i35xC59PspenwaMM1ji0tDTT1OrHTeQoBAZRq/559lmvkzpgZc/p/Xu0FVkTzmhW5ugmiTujXv9SM09EB6HqUSYxzFK6fjE4WknSEZTi6I7QQRCRGbTzhT4JRErzraKkE+9cZHkc+8n30ySrGhz8o/RdNX4j2AETrX5BcDSFqMBoAH+6PLxg23Ju0n1Dpu2fDO2Evyih93IrOriQuGpa7kb+0Q34w0+ParQQ6nxmP+sc2t+Kj+hwIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private String notify_url = "http://21dc7e37cu.bjhttp.cn/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    //day133：设置支付宝的支付收单时间
    private String timeout = "30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;
    }
}